﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;

using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.Db.DAL;
using SysTool._00_Def;
using SysTool._02_Service;

namespace SysTool
{
    public partial class Form1 : Form
    {
        private static ILog _logger = LogManager.GetLogger(typeof(Form1));

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(textBox1.Text);
                XmlNodeList xnl = xml.SelectNodes("//row");
                StringBuilder sb = new StringBuilder();
                using (var db = SqlSugarHelper.GetDb())
                {
                    foreach (XmlNode item in xnl)
                    {
                        var node1 = item.SelectSingleNode("./column-1");    // Id
                        var node2 = item.SelectSingleNode("./column-2");    // 姓名
                        var node3 = item.SelectSingleNode("./column-3");    // 手机号码
                        var node4 = item.SelectSingleNode("./column-4");    // 邀请码
                        var node6 = item.SelectSingleNode("./column-6");    // 推荐人邀请码
                        var node7 = item.SelectSingleNode("./column-7");    // 登录时间
                        var node8 = item.SelectSingleNode("./column-8");    // 注册时间

                        var ui = db.Queryable<UserInfoEntity>().Where(x => x.Name == node2.InnerText && x.Phone == node3.InnerText).First();
                        if (ui == null)
                        {
                            var newUserInfoEntity = new UserInfoEntity
                            {
                                Phone = node3.InnerText,
                                Password = "E10ADC3949BA59ABBE56E057F20F883E",
                                Name = node2.InnerText,
                                InvitationCode = node4.InnerText.ToUpper(),
                                ReferenceInvitationCode = node6.InnerText.ToUpper(),
                                CreateTime = Convert.ToDateTime(node8.InnerText),
                                LoginTime = Convert.ToDateTime(node7.InnerText)
                            };
                            db.Insertable<UserInfoEntity>(newUserInfoEntity).ExecuteCommand();
                        }
                    }

                    MessageBox.Show(this, "Import Success!", "SysTool", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "SysTool", MessageBoxButtons.OK);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                using (var db = SqlSugarHelper.GetDb())
                {
                    var ls = db.Queryable<ShopInfoEntity>().OrderByDescending(x => x.Id).ToList();
                    foreach (var item in ls)
                    {
                        var ls2 = QueryAllUsersWithSameShopByUserId(item.ShopManagerUserId);
                        foreach (var item2 in ls2)
                        {
                            var entity = db.Queryable<ShopUserEntity>().Where(x => x.UserId == item2.Id).First();
                            if (entity == null)
                            {
                                var entity2 = new ShopUserEntity { ShopManagerUserId = item.ShopManagerUserId, UserId = item2.Id };
                                int n = db.Insertable<ShopUserEntity>(entity2).ExecuteCommand();
                            }
                            else
                            {
                                entity.ShopManagerUserId = item.ShopManagerUserId;
                                db.Updateable<ShopUserEntity>(entity).ExecuteCommand();
                            }
                        }
                    }
                }

                MessageBox.Show(this, "Update Shop User Success!", "SysTool", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "SysTool", MessageBoxButtons.OK);
            }
        }

        private IList<UserInfoEntity> QueryAllUsersWithSameShopByUserId(int userId)
        {
            IList<UserInfoEntity> ls = new List<UserInfoEntity>();

            var shopService = AutoFacHelper.Get<IShopService>();
            var shopInfo = shopService.QueryShopInfoByUserId(userId);
            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<UserInfoEntity>().Where(x => x.Id == userId).First();
                if (entity != null)
                {
                    ls.Add(entity);

                    Queue<string> queue = new Queue<string>();
                    string referenceInvitationCode = entity.InvitationCode;

                    var ls2 = db.Queryable<UserInfoEntity>().Where(x => x.ReferenceInvitationCode == referenceInvitationCode).ToList();
                    foreach (var item in ls2)
                    {
                        var count = db.Queryable<ShopUserEntity>()
                            .Where(x => x.UserId == item.Id && x.ShopManagerUserId != shopInfo.ShopManagerUserId)
                            .Count();
                        if (count == 0)
                        {
                            ls.Add(item);
                            queue.Enqueue(item.InvitationCode);
                        }
                    }

                    while (queue.Count > 0)
                    {
                        string referenceInvitationCode2 = queue.Dequeue();
                        var ls3 = db.Queryable<UserInfoEntity>().Where(x => x.ReferenceInvitationCode == referenceInvitationCode2).ToList();
                        foreach (var item in ls3)
                        {
                            var count = db.Queryable<ShopUserEntity>()
                            .Where(x => x.UserId == item.Id && x.ShopManagerUserId != shopInfo.ShopManagerUserId)
                            .Count();
                            if (count == 0)
                            {
                                ls.Add(item);
                                queue.Enqueue(item.InvitationCode);
                            }
                        }
                    }
                }
            }

            return ls;
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private string GetOrderName()
        {
            string[] arr = new string[] {
                "玛瑙", "和田玉", "手镯", "貔貅"
            };
            int n = Common.RandTool.CreateRandValWithMinMax(0, 3);
            return arr[n];
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(720);
            for (int i = 0; i < ls.Count - 1; i++)
            {
                var oi = new OrderEntity
                {
                    Amount = Common.RandTool.CreateRandValWithMinMax(3000, 9000),
                    BuyerUserId = ls.ElementAt(i).UserInfo.Id,
                    SellerUserId = ls.ElementAt(i + 1).UserInfo.Id,
                    OrderNo = string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff")),
                    State = 0,
                    Visible = 1,
                    Name = GetOrderName(),
                    CreateTime = dateTimePicker3.Value
                };
                OrderService.AddOrder(oi);
            }
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(693);
            for (int i = 0; i < ls.Count - 1; i++)
            {
                var oi = new OrderEntity
                {
                    Amount = Common.RandTool.CreateRandValWithMinMax(3000, 9000),
                    BuyerUserId = ls.ElementAt(i).UserInfo.Id,
                    SellerUserId = ls.ElementAt(i + 1).UserInfo.Id,
                    OrderNo = string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff")),
                    State = 0,
                    Visible = 1,
                    Name = GetOrderName(),
                    CreateTime = dateTimePicker1.Value
                };
                OrderService.AddOrder(oi);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(698);
            for (int i = 0; i < ls.Count - 1; i++)
            {
                var oi = new OrderEntity
                {
                    Amount = Common.RandTool.CreateRandValWithMinMax(3000, 9000),
                    BuyerUserId = ls.ElementAt(i).UserInfo.Id,
                    SellerUserId = ls.ElementAt(i + 1).UserInfo.Id,
                    OrderNo = string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff")),
                    State = 0,
                    Visible = 1,
                    Name = GetOrderName(),
                    CreateTime = dateTimePicker2.Value
                };
                OrderService.AddOrder(oi);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(703);
            for (int i = 0; i < ls.Count - 1; i++)
            {
                var oi = new OrderEntity
                {
                    Amount = Common.RandTool.CreateRandValWithMinMax(3000, 9000),
                    BuyerUserId = ls.ElementAt(i).UserInfo.Id,
                    SellerUserId = ls.ElementAt(i + 1).UserInfo.Id,
                    OrderNo = string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff")),
                    State = 0,
                    Visible = 1,
                    Name = GetOrderName(),
                    CreateTime = dateTimePicker4.Value
                };
                OrderService.AddOrder(oi);
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(710);
            for (int i = 0; i < ls.Count - 1; i++)
            {
                var oi = new OrderEntity
                {
                    Amount = Common.RandTool.CreateRandValWithMinMax(3000, 9000),
                    BuyerUserId = ls.ElementAt(i).UserInfo.Id,
                    SellerUserId = ls.ElementAt(i + 1).UserInfo.Id,
                    OrderNo = string.Format("E{0}", DateTime.Now.ToString("yyyyMMddHHmmssfffff")),
                    State = 0,
                    Visible = 1,
                    Name = GetOrderName(),
                    CreateTime = dateTimePicker5.Value
                };
                OrderService.AddOrder(oi);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(710);
            for (int i = 0; i < ls.Count; i++)
            {
                var ai = new AppointmentEntity
                {
                    Type = "3-9k",
                    Time = dateTimePicker5.Value,
                    UserId = ls.ElementAt(i).UserInfo.Id
                };
                AppointmentService.Add(ai);
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(710);
            for (int i = 0; i < ls.Count; i++)
            {
                var ai = new AppointmentEntity
                {
                    Type = "3-9k",
                    Time = dateTimePicker4.Value,
                    UserId = ls.ElementAt(i).UserInfo.Id
                };
                AppointmentService.Add(ai);
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(710);
            for (int i = 0; i < ls.Count; i++)
            {
                var ai = new AppointmentEntity
                {
                    Type = "3-9k",
                    Time = dateTimePicker3.Value,
                    UserId = ls.ElementAt(i).UserInfo.Id
                };
                AppointmentService.Add(ai);
            }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(710);
            for (int i = 0; i < ls.Count; i++)
            {
                var ai = new AppointmentEntity
                {
                    Type = "3-9k",
                    Time = dateTimePicker2.Value,
                    UserId = ls.ElementAt(i).UserInfo.Id
                };
                AppointmentService.Add(ai);
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            var ls = ShopUserService.QueryShopAllUsers(710);
            for (int i = 0; i < ls.Count; i++)
            {
                var ai = new AppointmentEntity
                {
                    Type = "3-9k",
                    Time = dateTimePicker1.Value,
                    UserId = ls.ElementAt(i).UserInfo.Id
                };
                AppointmentService.Add(ai);
            }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            var form = new Form2();
            form.ShowDialog();
        }

        private void button15_Click(object sender, EventArgs e)
        {
            var form = new Form3();
            form.ShowDialog();
        }
    }
}
